"""
飞书数据模型定义
"""

from dataclasses import dataclass
from typing import Dict, List, Optional, Any
from datetime import datetime


@dataclass
class FeishuWebhookMessage:
    """飞书Webhook消息模型"""
    challenge: Optional[str] = None
    token: Optional[str] = None
    type: Optional[str] = None
    schema: Optional[str] = None
    header: Optional[Dict[str, Any]] = None
    event: Optional[Dict[str, Any]] = None
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'FeishuWebhookMessage':
        return cls(
            challenge=data.get('challenge'),
            token=data.get('token'),
            type=data.get('type'),
            schema=data.get('schema'),
            header=data.get('header'),
            event=data.get('event')
        )


@dataclass
class FeishuMessageEvent:
    """飞书消息事件模型"""
    message_id: str
    chat_type: str
    message_type: str
    content: str
    sender: Dict[str, Any]
    create_time: str
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'FeishuMessageEvent':
        return cls(
            message_id=data.get('message_id', ''),
            chat_type=data.get('chat_type', ''),
            message_type=data.get('message_type', ''),
            content=data.get('content', ''),
            sender=data.get('sender', {}),
            create_time=data.get('create_time', '')
        )


@dataclass
class FeishuOAuthToken:
    """飞书OAuth Token模型"""
    access_token: str
    token_type: str
    expires_in: int
    name: str
    en_name: str
    avatar_url: str
    avatar_thumb: str
    avatar_middle: str
    avatar_big: str
    open_id: str
    union_id: str
    email: str
    enterprise_email: str
    user_id: str
    mobile: str
    tenant_key: str
    refresh_token: str
    refresh_expires_in: int
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'FeishuOAuthToken':
        return cls(
            access_token=data.get('access_token', ''),
            token_type=data.get('token_type', ''),
            expires_in=data.get('expires_in', 0),
            name=data.get('name', ''),
            en_name=data.get('en_name', ''),
            avatar_url=data.get('avatar_url', ''),
            avatar_thumb=data.get('avatar_thumb', ''),
            avatar_middle=data.get('avatar_middle', ''),
            avatar_big=data.get('avatar_big', ''),
            open_id=data.get('open_id', ''),
            union_id=data.get('union_id', ''),
            email=data.get('email', ''),
            enterprise_email=data.get('enterprise_email', ''),
            user_id=data.get('user_id', ''),
            mobile=data.get('mobile', ''),
            tenant_key=data.get('tenant_key', ''),
            refresh_token=data.get('refresh_token', ''),
            refresh_expires_in=data.get('refresh_expires_in', 0)
        )


@dataclass
class FeishuUserInfo:
    """飞书用户信息模型"""
    user_id: str
    union_id: str
    open_id: str
    name: str
    en_name: str
    email: str
    mobile: str
    avatar_url: str
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'FeishuUserInfo':
        return cls(
            user_id=data.get('user_id', ''),
            union_id=data.get('union_id', ''),
            open_id=data.get('open_id', ''),
            name=data.get('name', ''),
            en_name=data.get('en_name', ''),
            email=data.get('email', ''),
            mobile=data.get('mobile', ''),
            avatar_url=data.get('avatar_url', '')
        )
